package com.simplemonkey.widgets;

import com.jme.math.Vector3f;
import com.jme.scene.Spatial;
import com.simplemonkey.IResizeListener;
import com.simplemonkey.Widget;
import com.simplemonkey.util.Dimension;

/**
 * A simple widget that just wraps a spatial
 * @author Sam
 *
 */
public class SpatialWidget extends Widget {
	
	private Spatial spatial;
	
	/**
	 * Create a widget that does not apply resizing.
	 * @param name
	 * @param spatial
	 */
	public SpatialWidget(String name, Spatial spatial) {
		this(name,spatial,false);
	}
	
	/**
	 * 
	 * @param name
	 * @param spatial
	 * @param applyResizing Whether or not to scale the spatial to match the size of the widget.
	 */
	public SpatialWidget(String name, Spatial spatial, boolean applyResizing) {
		super(name);
		this.spatial = spatial;
		
		if(applyResizing)
		{
			super.addResizeListener(new IResizeListener()
			{
				
				public void resize(Dimension newSize) {
					getSpatial().getLocalScale().set(getSpacingAppearance().getContentWidth(),getSpacingAppearance().getContentHeight(),1f);
					getSpatial().updateGeometricState(0, true);
				}
			});
		}
	}

	

	
	public void setPosition(Vector3f p) {
		
		this.getSpatial().getLocalTranslation().set(p).addLocal(this.getWidth()/2f, this.getHeight()/2f, 0);
	
		this.getSpatial().updateGeometricState(0, true);
	}

	
	public Spatial getSpatial() {
		return spatial;
	}

}
